package JDBC;

import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.CountDownLatch;

public class MysqlPoolController {
    private CountDownLatch countDownLatch = new CountDownLatch(15);
    @Autowired
    private JDBCpool mysqlPool;

    private final static String SQL = "INSERT INTO DuridPool(name) VALUES('Zhangsan')";

    public static int business(Connection conn, String sql){
        try {
            Statement statement = conn.createStatement();
            return statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getPoolConn( int times)  {
        System.out.println(times + "次请求连接，请求线程：" + Thread.currentThread().getName());
        int count = 0;
        for (int i=0;i<times;i++) {
            Connection conn = mysqlPool.getConnection();
            count = business(conn, SQL);
            System.out.println("插入数据成功");
            mysqlPool.release(conn);
        }
        if (count > 0) {
            return "插入数据成功";
        }
        return "插入数据失败";
    }

    private void getPoolConnTime()  {
        long start = System.currentTimeMillis();
        getPoolConn(10000);
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("getPoolConn:"+time);
    }
    private int bfCount = 0;

    /**
     * 并发使用连接池测试
     * @param times
     * @return
     */
    public String getPoolBfConn( int times) {
        System.out.println(times + "次请求连接，请求线程：" + Thread.currentThread().getName());
        int count = 0;
        new Thread(new Runnable() {
            @Override
            public void run() {
                countDownLatch.countDown();

                try {
                    countDownLatch.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                for (int i = 0; i < times; i++) {
                    Connection conn = null;
                    try {
                        conn = mysqlPool.getConnection();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    bfCount = business(conn, SQL);
                    System.out.println("插入数据成功");
                    mysqlPool.release(conn);
                }
            }
        }).start();
        if (bfCount > 0) {
            return "插入数据成功";
        }
        return "插入数据失败";
    }
    private void getPoolBfConnTime() {
        long start = System.currentTimeMillis();
        getPoolBfConn(10000);
        long end = System.currentTimeMillis();
        long time = end - start;
        System.out.println("getPoolBfConnTime:"+time);
    }

    public static void main(String[] args) throws Exception {
        MysqlPoolController mysql = new MysqlPoolController();
        mysql.getPoolConnTime();
        mysql.getPoolBfConnTime();

    }
}
